	//
	//  GaoCvImageLabelCell.m
	//  OpenglDemo
	//
	//  Created by Gikki Ares on 2020/6/24.
	//



#import "JsMenuDefaultItemView.h"

@interface Prefix(MenuDefaultItemView)()
{
	UILabel *mlbl_title;
	UIImageView *miv;
	UIImage * mimage_selected;
	UIImage * mimage_unselected;
}

@end

@implementation Prefix(MenuDefaultItemView)

- (instancetype)init
{
	self = [super init];
	if (self) {
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		[self commonInit];
	}
	return self;
}

- (void)awakeFromNib {
	[super awakeFromNib];
		// Initialization code
	[self commonInit];
	
}


- (void) commonInit {
	miv = [[UIImageView alloc]init];
	[self addSubview:miv];
	mlbl_title = [[UILabel alloc]init];
	[self addSubview:mlbl_title];
	mlbl_title.textAlignment = NSTextAlignmentCenter;
}

- (void)setVm:(JsMenuItemVm *)vm {
	_vm = vm;
	[self setupUi];
}

- (void)setupUi {
	
	JsMenuItemStyle * style = [self.vm currentStyle];
	
	
			//只有标题,label占满显示
		if(_vm.title.length&&!_vm.imageName.length) {
			mlbl_title.frame = self.bounds;
		}
		else {
				//标题的高度固定, space总共0.3,title0.3,image 0.4
			CGFloat mf_height_cell = self.bounds.size.height;
			CGFloat mf_height_title = mf_height_cell * 0.3;
			CGFloat mf_height_space = mf_height_cell * 0.1;
			CGFloat mf_size_image = mf_height_cell * 0.4;
			
				//图片位置
			CGFloat mf_x_image = (self.bounds.size.width - mf_size_image)/2;
			CGFloat mf_y_image = mf_height_space;
			miv.frame = CGRectMake(mf_x_image, mf_y_image, mf_size_image, mf_size_image);
			
				//标题
			CGFloat mf_x_title = 0;
			CGFloat mf_y_title = mf_height_space + mf_size_image + mf_height_space;
			mlbl_title.frame = CGRectMake(mf_x_title,mf_y_title, self.bounds.size.width, mf_height_title);
			mlbl_title.text = _vm.title;
		}
	

		//只有标题,label占满显示
	if(_vm.title.length&&!_vm.imageName.length) {
		mlbl_title.text = _vm.title;
		mlbl_title.textColor = style.titleColor;
		mlbl_title.font = style.titleFont;
		mlbl_title.numberOfLines = 0;
	}
		//有标题和图片
	else {
//		mimage_selected = [self imageWithName:model->mstr_imageName_selected];
//		mimage_unselected = [self imageWithName:model->mstr_imageName_unselected];
		mimage_selected = [self imageWithName:_vm.imageName];
		mimage_unselected = [self imageWithName:_vm.imageName];
	}


	self.backgroundColor = style.cellBackgroundColor;
	self.layer.borderColor = style.borderColor.CGColor;
	self.layer.cornerRadius = style.cornerRadius;
	self.layer.borderWidth = style.borderWidth;

	
	//需要没有开启的时候去掉,否则会出现异常.
	if(style.isUseDefaultShadow) {
		self.layer.shadowColor = [UIColor blackColor].CGColor;
		self.layer.shadowRadius = 5;
		self.layer.shadowOffset = CGSizeMake(3, 3);
		self.layer.shadowOpacity = 0.8;
	}
	else {
		self.layer.shadowColor = nil;
		self.layer.shadowRadius = 0;
		self.layer.shadowOffset = CGSizeZero;
		self.layer.shadowOpacity = 1;
	}

	/**
	 怎么关闭设置字体的动画效果?,显示选中和未选中的style
	 */
	[UIView performWithoutAnimation:^{
			miv.image = mimage_selected;
			[mlbl_title setFont:style.titleFont];
			mlbl_title.font = style.titleFont;
			mlbl_title.textColor = style.titleColor;
	}];
}

- (void)setSelected:(BOOL)selected {
	[super setSelected:selected];
	if(self.selected) {
		miv.image = mimage_selected;
	}
	else {
		miv.image = mimage_unselected;
	}
}

- (UIImage *)imageWithName:(NSString *)imageName {
	UIImage * image = nil;
#if 1
	image = [UIImage imageNamed:imageName];
#else
	image = [[UIImage alloc]initWithContentsOfFile:imageName];
#endif
	return image;
}

@end
